package com.sznaner.tool.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


import java.util.List;

public class AppUtil {


    //是否安装某个app
    public static boolean checkAppInstalled(String pkgName) {
        PackageManager manager = NRTool.context().getApplicationContext().getPackageManager();
        List<PackageInfo> pkgList = manager.getInstalledPackages(0);
        for (int i = 0; i < pkgList.size(); i++) {
            PackageInfo pI = pkgList.get(i);
            if (pI.packageName.equals(pkgName))
                return true;
        }
        return false;
    }

    public static void startApp(String packageName) {
        Intent intent = getLaunchIntentForPackage(packageName);
        if (intent != null) {
            NRTool.context().startActivity(intent);
        }
    }

    public static void startApp(String packageName,String mainActivity) {
        Intent intent = new Intent();
        ComponentName cn = new ComponentName(packageName, mainActivity);
        intent.setComponent(cn);
        if (intent != null) {
            NRTool.context().startActivity(intent);
        }
    }
    public static void startActivity(Intent intent) {
        if (intent != null) {
            NRTool.context().startActivity(intent);
        }
    }

    //获取应用程序版本名称信息
    public static String getPackageName() {
        try {
            PackageManager packageManager =  NRTool.context().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    NRTool.context().getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Intent getLaunchIntentForPackage(String packageName) {
        PackageManager packageManager = NRTool.context().getPackageManager();
        return packageManager.getLaunchIntentForPackage(packageName);
    }


    /**
     * 获取当前app version name(即版本号)
     */
    public static String getCurAppVersion() {
        String appVersionName = "";
        Context context = NRTool.context();
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            appVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return appVersionName;
    }


    //获取指定app版本号
    public static String getAppVersion(String packageName) {
        String appVersionName = "0.0.1";//防止获取未安装app版本号,造成异常
        try {
            PackageInfo packageInfo = NRTool.context().getPackageManager().getPackageInfo(packageName, 0);
            appVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return appVersionName;
    }

}
